<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>数据集</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui/css/layui.css">
    <link rel="stylesheet" href="../css/scroll-bar.css">
    <link rel="stylesheet" href="../css/sub-page.css">
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../lib/nprogress/nprogress.css">
</head>

<body>
    <div class="ok-body">
        <!--面包屑导航区域-->
        <div class="ok-body-breadcrumb">
            <span class="layui-breadcrumb">
                <a><cite>首页</cite></a>
                <a><cite>数据集管理</cite></a>
                <a><cite>数据集</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" href="javascript:location.replace(location.href);" title="刷新">
                <i class="layui-icon layui-icon-refresh"></i>
            </a>
        </div>
        <!--模糊搜索区域-->
        <div class="layui-row ok-search">
            <input class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime">
            <input class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime">
            <div class="layui-inline">
                <input class="layui-input" name="id" id="dsname" autocomplete="off" placeholder="请输入数据集名称">
            </div>
            <div class="layui-inline">
                <input class="layui-input" name="id" id="dstags" autocomplete="off" placeholder="数据集标签">
            </div>

            <button class="layui-btn" data-type="reload" id="search">搜索</button>
        </div>
        <!--工具栏-->
        <okToolbar>
            <!--   <button class="layui-btn layui-btn-normal" id="batchEnabled">
                <i class="iconfont icon-shangsheng"></i>批量启用
            </button>
            <button class="layui-btn layui-btn-warm" id="batchDisabled">
                <i class="iconfont icon-web-icon-"></i>批量停用
            </button>
            <button class="layui-btn layui-btn-danger" id="batchDel">
                <i class="layui-icon layui-icon-delete"></i>批量删除
            </button> -->
            <button class="layui-btn" id="add">
                <i class="layui-icon">&#xe61f;</i>一键选取
            </button>
            <button class="layui-btn layui-hide layui-bg-red" id="add_super">
                <i class="layui-icon">&#xe61f;</i>高级筛选
            </button>
            <span>共有数据：<i id="countNum">0</i> 条</span>
        </okToolbar>
        <!--数据表格-->
        <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
        <input class="layui-hide" id='id' value="">
    </div>
    <!--js逻辑-->
    <script src="../lib/layui/layui.js"></script>
    <script src="../lib/nprogress/nprogress.js"></script>
    <script>
        NProgress.start();
    window.onload = function () {
        NProgress.done();
    }
    var jquery;
    var searchitem=[];
    layui.use(['element', 'table', 'form', 'jquery','laydate'], function () {
        var element = layui.element;
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var laydate = layui.laydate;
        jquery=$;
        laydate.render({
            elem: '#startTime',
            type: "datetime"
        });

        laydate.render({
            elem: '#endTime',
            type: "datetime"
        });
        table.render({
            elem: '#tableId',
            url: '/dataset/list',
            limit: 10,
            cellMinWidth: 80,            
            where:{
                        key:{
                            status:1
                        }
                    },
            page: true,
            id:'folderlist',
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'dsid', title: 'ID', width: 80, sort: true},
                {field: 'dsname', title: '数据集'},
                {field: 'totalcount', title: '数据总量',width:90},
                {field: 'dstags', title: '数据集标签'},
                {field: 'dstype', title: '数据源',templet: '#typeTpl'},
                {field: 'description', title: '描述',width:120},
                {field: 'createat', title: '创建时间'},
                {field: 'status', title: '状态', width: 95, templet: '#statusTpl'},
                {field: 'actions',title: '类型',templet: '#actionTpl'},
                {field: 'createtype', title: '创建方式',width:95,  templet: '#createtypeTpl'},
                {title: '操作', templet: '#settingTpl', align: 'center'}
            ]],
            done: function (res, curr, count) {
                //console.log(res);
                $("#countNum").text(count);
                table_data=res.data;
                for(var i=0;i<res.data.length;i++){
                    for(var j=0;j<searchitem.length;j++){
                        if(res.data[i].dsid==searchitem[j].dsid){
                            res.data[i]["LAY_CHECKED"]=true;
                            var index=i;
                            $('.layui-table-fixed-l tr[data-index=' + index + '] input[type="checkbox"]')["LAY_CHECKED"]='true';
                            $('.layui-table-fixed-l tr[data-index=' + index + '] input[type="checkbox"]').prop('checked', true);
                            $('.layui-table-fixed-l tr[data-index=' + index + '] input[type="checkbox"]').next().addClass('layui-form-checked');
                            break;
                        }
                    }
                }
                
            
            }
        });

        var active = {
            reload: function(){
                var dsname = $('#dsname').val();
                var dstags = $('#dstags').val();
                var start=$('#startTime').val();
                var end=$('#endTime').val();
                
                //执行重载
                table.reload('folderlist', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key:{
                            dsname:dsname,
                            dstags:dstags,
                            start:start,
                            end:end,
                            status:1                           
                        }
                    }
                });
            }
        };

        $('#search').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('#dsname,#dstags').on('keydown',function(e){
            if(e.keyCode=='13'){
                $('#search').click();
            }
        });
      

         $("#add").click(function () {
             //获取选中的数据行
             var checkStatus = table.checkStatus('folderlist');
             var data = checkStatus.data;
             //layer.alert(JSON.stringify(data));
             var dsids=[];
             data.forEach(element => {
                 dsids.push(element.dsid);
             });
             if(dsids.length==0){
                layer.alert('请选择数据集');
                return;
             }
             //layer.alert(JSON.stringify(dsids));
             layer.open({
                type: 1
                ,title: false //不显示标题栏
                ,closeBtn: false
                ,area: '300px;'
                ,shade: 0.8
                ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                //,btn: ['火速围观', '残忍拒绝']
                ,btnAlign: 'c'
                ,moveType: 1 //拖拽模式，0或者1
                ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">你知道吗？亲！<br>我正在努力的处理中...</br><br>每一次都是浩瀚数据的汇集，短暂等待只为更佳美好的呈现</br><br>处理完毕后我将消失不见...</br><br>我们此后的征途是星辰大海 ^_^</br></div>'
                /* ,success: function(layero){
                var btn = layero.find('.layui-layer-btn');
                btn.find('.layui-layer-btn0').attr({
                        href: 'http://www.layui.com/'
                        ,target: '_blank'
                    });
                } */
            });
            //提交选择信息
            $.ajax({
                    url: '/dataset/binddataset',
                    method: 'post',
                    data: {dsid:$('#id').val(),ids:dsids,dstype:0},
                    dataType: 'JSON',
                    success: function (res) {
                        console.log(res);
                        if (res.code == 1) {
                            layer.msg('操作成功');
                            parent.layer.closeAll();
                        } else if (res.code == -1)  {
                            layer.msg('不能重复添加');
                        }else{
                            layer.msg('操作失败');
                        }
                    },
                    error: function (data) {}
                }); 
        });

            $("#add_super").click(function () {
             //获取选中的数据行
             var checkStatus = table.checkStatus('folderlist');
             var data = checkStatus.data;
             //layer.alert(JSON.stringify(data));
             var dsids=[];
             data.forEach(element => {
                 dsids.push(element.dsid);
             });
             if(dsids.length==0){
                layer.alert('请选择数据集');
                return;
             }
             //layer.alert(JSON.stringify(dsids));
             //layer.alert(JSON.stringify(searchitem));
             var submit_ids=[];
             //获取等提交的数据
             for (let i = 0; i < dsids.length; i++) {
                 const dsid = dsids[i];
                 for (let j = 0; j < searchitem.length; j++) {
                 const item = searchitem[j];
                 if(dsid==item.dsid){//已选择
                    submit_ids.push(item);
                    break;
                 }                 
             }
             }
             
            //提交选择信息,进入数据过滤，并生成新的数据集
             $.ajax({
                    url: '/dataset/binddataset_search',
                    method: 'post',
                    data: {dsid:$('#id').val(),ids:JSON.stringify(submit_ids),dstype:0},
                    dataType: 'JSON',
                    success: function (res) {
                        console.log(res);
                        if (res.code == 1) {
                            layer.msg('操作成功');
                            parent.layer.closeAll();
                        } else if (res.code == -1)  {
                            layer.msg('不能重复添加');
                        }else{
                            layer.msg('操作失败');
                        }
                    },
                    error: function (data) {}
                });  
        });
          table.on('tool(tableFilter)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'condition') {
                layer.open({
                    title: '数据过滤-'+data.dsname,
                    type: 2,
                    shade: false,
                    maxmin: true,
                    shade: 0.5,
                    area: ['90%', '90%'],
                    content: '/dataset/condition',
                    zIndex: layer.zIndex,
                    end: function () {
                        $(".layui-laypage-btn")[0].click();
                    },
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        
                        for (let index = 0; index < searchitem.length; index++) {
                            const element = searchitem[index];
                            if(data.dsid==element.dsid){
                                body.find('#data').val(JSON.stringify(element.data));
                                console.log(body.find('#data').val());
                                break;
                            }
                            
                        }
                        body.find('#id').val(data.dsid);
                        console.log(body.find('#id').val());
                    }
                });
            } 
    });
});


        function callback(dsid,data) {
            
            console.log(dsid);
            console.log(data);


               var obj=[];
        //过滤出参数不为空的内容            
            for (var p in data) {
                var name = p;//属性名称 
                var value = data[p];//属性对应的值 
                var item={};
                if(p.indexOf('tag')>-1){
                    item.tag=value;
                    obj.push(item); 
                }else{
                    item.condition=value;
                    obj.push(item);  
                }                             
            }

            var condition_data=[];
            for (let index = 0; index < obj.length; index++) {
                condition_data.push({'tag':obj[index].tag,'condition':obj[++index].condition});    
            }
            var exists=false;
            var idx=0;
            for (let index = 0; index < searchitem.length; index++) {
                            const element = searchitem[index];
                            if(dsid==element.dsid){
                               exists=true;
                               idx=index;
                                break;
                            }
                            
                        }
            if(exists){
                searchitem[idx].data=condition_data;
            }else{
                searchitem.push({dsid:dsid,data:condition_data});
            }
            if(searchitem.length>0){
                jquery('#add').addClass('layui-hide');
                jquery('#add_super').removeClass('layui-hide');
            }

            console.log(searchitem);
        }
</script>
    <!--模板-->
    <script type="text/html" id="statusTpl">
        <a href="javascript:;" title="状态" class="layui-btn layui-btn-xs  {{ d.status== 0? 'layui-bg-orange' : 'layui-bg-green'}} ">
              {{#  if(d.status == 0){ }}
                未完成
              {{#  } }}
              {{#  if(d.status == 1){ }}
              创建完成
              {{#  } }}
              {{#  if(d.status == 2){ }}
                已发布
              {{#  } }}
        </a>
   
</script>

    <script type="text/html" id="typeTpl">
        <a href="javascript:;" title="数据源" lay-event="{{ d.dstype== -1 ? 'bind' : ''}}"  class="layui-btn layui-btn-xs {{ d.dstype== -1 ? 'layui-bg-red' : 'layui-bg-green'}}">
        {{#  if(d.dstype == 0){ }}
            数据集
          {{#  } }}
          {{#  if(d.dstype == 1){ }}
            文件系统
          {{#  } }}
          {{#  if(d.dstype == -1){ }}
            源未设置
          {{#  } }}
    </a>
</script>
    <script type="text/html" id="operationTpl">
        <a href="javascript:;" title="上传文件" lay-event="bind" class="layui-btn layui-btn-xs layui-bg-blue">绑定源</a>
        <a href="javascript:;" title="文件列表" lay-event="list" class="layui-btn layui-btn-xs layui-bg-blue">设置</a>
         <!--<a href="javascript:;" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a> -->
</script>
    <script type="text/html" id="createtypeTpl">
        <a href="javascript:;" title="数据源" class="layui-btn layui-btn-xs {{ d.createtype== 1 ? 'layui-bg-green' : 'layui-bg-black'}}">{{ d.createtype== 1 ? '用户创建' : '系统生成'}}
</a>
</script>

    <script type="text/html" id="settingTpl">
        <a href="javascript:;" title="高级检索" class="layui-btn layui-btn-xs layui-bg-red" lay-event="condition">高级检索
</a>
</script>
<script type="text/html" id="actionTpl">
    <a href="javascript:;" title="执行类别"  class="layui-btn layui-btn-xs">
    {{#  if(d.actions === 0){ }}
    数据筛选
    {{#  } }}     
   {{#  if(d.actions === 1){ }}
    单分类
    {{#  } }}
    {{#  if(d.actions === 2){ }}
    多分类
    {{#  } }}  
    {{#  if(d.actions === -1){ }}
    未知
    {{#  } }}  
</a>
</body>

</html>